﻿using Microsoft.OpenApi.Any;
using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.SwaggerGen;

namespace DifyToOpenAI.CustSwagger
{
    // 实现 IOperationFilter 接口的类
    public class RequestBodyFilter : IOperationFilter
    {
        public void Apply(OpenApiOperation operation, OperationFilterContext context)
        {
            // 检查方法名是否为 AutoExec
            //if (context.ApiDescription.ActionDescriptor.DisplayName!.Contains("AutoExec"))
            //if (context.ApiDescription.ActionDescriptor.DisplayName != null && context.ApiDescription.ActionDescriptor.DisplayName.IndexOf("AutoExec", StringComparison.OrdinalIgnoreCase) >= 0
            //    && "POST".Equals(context.ApiDescription.HttpMethod!, StringComparison.OrdinalIgnoreCase)
            //    )
            if ("POST".Equals(context.ApiDescription.HttpMethod!, StringComparison.OrdinalIgnoreCase))

            {
                // 检查方法是否有 [FromBody] 参数
                //var fromBodyParameters = context.ApiDescription.ParameterDescriptions
                //   .Where(p => p.Source.Id == "Body")
                //   .ToList();
                if (operation.RequestBody == null)
                {
                    operation.RequestBody = new OpenApiRequestBody
                    {
                        Description = "Any content",
                        Required = false,
                        Content = new Dictionary<string, OpenApiMediaType>
                {
                    {
                        "application/json", new OpenApiMediaType
                        {
                            Schema = new OpenApiSchema
                            {
                                Type = "string",
                                AdditionalPropertiesAllowed = true,

                            }
                        }
                    },

                    {
                        "text/plain", new OpenApiMediaType
                        {
                            Schema = new OpenApiSchema
                            {
                                Type = "string"
                            }
                        }
                    },

                        {
                        "application/x-www-form-urlencoded", new OpenApiMediaType
                        {
                             Schema = new OpenApiSchema
                            {
                                Type = "object",
                                Properties = new Dictionary<string, OpenApiSchema>
                            {
                                // 定义 formbody 中的字段
                                //{"datasource", new OpenApiSchema { Type = "string", Default = new OpenApiString("swagger") } },
                                //{"formid", new OpenApiSchema { Type = "string" } },
                                //{"jsondata", new OpenApiSchema { Type = "string" } }
                            }
                            }
                        }
                    }
                }
                    };
                }
            }
        }
    }
}
